﻿using EFCore_CodeFirst_ForDocker_MVC.Contexts;
using EFCore_CodeFirst_ForDocker_MVC.Models;
using Microsoft.AspNetCore.Mvc;
using System.Text;
using System.Text.RegularExpressions;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace EFCore_CodeFirst_ForDocker_MVC.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        public DockerDbContext db;

        public ValuesController(DockerDbContext db)
        {
            this.db = db;
        }

        // GET: api/<ValuesController>
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/<ValuesController>/5
        [HttpGet("{id}")]
        public string Get(int id)
        {
            try
            {
                
                string folderPath = AppDomain.CurrentDomain.BaseDirectory + @"hlhl";
                //string folderPath = AppDomain.CurrentDomain.BaseDirectory + @"纠错重采";
                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
                string pattern = @"\d+$", a = "", a2 = "", a3 = "", a4 = "", a1 = "";
                DirectoryInfo directoryInfo = new DirectoryInfo(folderPath);
                DirectoryInfo directoryInfo2 = null;
                string[] filealist = null;
                Scientists_Intro si = null;
                var entities = new List<Scientists_Intro>();

                if (directoryInfo.Exists)
                {
                    foreach (var subFolder in directoryInfo.GetDirectories())
                    {
                        a = subFolder.FullName;a2 = ""; a3 = ""; a4 = ""; a1="";
                        filealist = Directory.GetFiles(a);
                        bool isNumeric = false;
                        foreach (var subFolder2 in filealist)
                        {
                            //string fileName = Path.GetFileName(subFolder2); //完整名
                            isNumeric = Regex.IsMatch(Path.GetFileNameWithoutExtension(subFolder2), @"^\d+$");
                            if (Path.GetExtension(subFolder2) == ".txt")
                            {
                                if (isNumeric)
                                {
                                    a2 = Path.GetFileNameWithoutExtension(subFolder2); //不包括扩展名
                                    a3 = System.IO.File.ReadAllText(subFolder2, Encoding.GetEncoding("GB2312"));
                                }
                                else
                                {
                                    a4 = System.IO.File.ReadAllText(subFolder2, Encoding.GetEncoding("GB2312"));
                                }
                            }
                            else if (Path.GetExtension(subFolder2).ToLower() == ".jpg") {
                                a1 = "havepic";
                            }
                        }
                        if (!string.IsNullOrEmpty(a2))
                        {
                            si = new Scientists_Intro();
                            si.Id = Convert.ToInt32(a2);
                            si.Scientists_Intro_Str = a3;
                            si.Scientists_Intro_Stren = a4;
                            si.Name = a1;
                            //插入数据库

                            // 填充entities列表...  
                            entities.Add(si);
                        }
                        else
                        {
                            Console.WriteLine(string.Format("{0}出错", subFolder));
                        }
                        

                        // 只跑某文件夹下特定的基金会 part2
                    }
                    db.Scientists_Intro.AddRange(entities);
                    db.SaveChanges();
                }
                else
                {
                    Console.WriteLine("该文件夹不存在！");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("发生错误：" + ex.Message);
            }
            return "value";
        }

        // POST api/<ValuesController>
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        // PUT api/<ValuesController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/<ValuesController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
